﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Data;
using System.IO;
using System.Collections;

using TRPOP.Common;
using TRPOP.CMS.Plugin;

namespace TRPOP.CMS.Template.GenerateCS
{

    #region 根据cs代码生成dll  根据dll，编译执行，获取值

    /// <summary>
    ///  根据cs代码生成dll  根据dll，编译执行，获取值
    /// </summary>
    public class GenerateCodeFile
    {
        
        /// <summary>
        /// 根据模板对应的cs文件生成dll
        /// </summary>
        /// <param name="strFileName"></param>
        /// <returns></returns>
        public static bool GenerateDll(string strFileName)
        {
            string strDllName = strFileName.Replace(".cs", ".dll");
            string strCsharpCode = "";
            if (FileUtil.IsExist(strFileName))
            {
                strCsharpCode = FileUtil.ReadFile(strFileName);
            }
            else
                return false;
            //创建编译器实例  
            CodeDomProvider oComplier = CodeDomProvider.CreateProvider("C#");
            //设置编译参数 
            CompilerParameters oParas = new CompilerParameters(null, strDllName);
            //添加引用的dll
            BaseOperUsingDlls(oParas);
            //生成可执行文件
            oParas.GenerateExecutable = false;
            //在内存中生成输出
            oParas.GenerateInMemory = false;

            //编译代码                          
            CompilerResults result = oComplier.CompileAssemblyFromSource(oParas, strCsharpCode);
            oComplier.Dispose();
            return true;
        }

        /// <summary>
        /// 必须要引用的dll文件
        /// </summary>
        /// <param name="oParas"></param>
        private static void BaseOperUsingDlls(CompilerParameters oParas)
        {
            oParas.ReferencedAssemblies.Add("System.dll");
            oParas.ReferencedAssemblies.Add("System.Data.dll");
            oParas.ReferencedAssemblies.Add("System.Web.dll");
            oParas.ReferencedAssemblies.Add("System.Xml.dll");
            string[] Files = Directory.GetFiles(Common.Util.GetMapPath("/bin"), "*.dll", SearchOption.TopDirectoryOnly);
            foreach (string strTemp in Files)
            {
                oParas.ReferencedAssemblies.Add(strTemp);
            }
        }

    }

    #endregion


    /// <summary>
    ///  dll模板程序所使用到的实体类
    /// </summary>
    public class TemplateDllPageInfo
    {
        /// <summary>
        /// 页面类型
        ///    0 什么也没有，发布失败
        ///    1 生成一个页面
        ///    2 生成多个页面
        /// </summary>
        public int intHtmlType = 0;

        /// <summary>
        /// 当前信息ID
        /// </summary>
        public int intCurrentContentID = 0;
        /// <summary>
        /// 当前节点ID
        /// </summary>
        public int intCurrentNodeID = 0;
        /// <summary>
        /// 当前节点的父ID
        /// </summary>
        public int intCurrentNodeParentID = 0;
        /// <summary>
        /// 分页的最大程度
        /// </summary>
        public int intContentPageLength = int.MaxValue;
        /// <summary>
        /// 当前分页数据不存在
        /// </summary>
        public bool IsCurrentPageDataError = false;

        /// <summary>
        /// 是否分页
        /// </summary>
        public bool IsFindPager = false;
        /// <summary>
        /// 分页已操作过了
        /// </summary>
        public bool IsPagerOpered = false;
        /// <summary>
        /// 分页大小
        /// </summary>
        public int intPageSize = 0;
        /// <summary>
        /// 列表页,生成页数，一般情况下建议5页以内 
        /// </summary>
        public int intListPagePubCount = 5;
        /// <summary>
        /// 分页显示的页数
        /// </summary>
        public int intShowPageNum = 10;
        /// <summary>
        /// 分页规则
        /// </summary>
        public string strlistPageRegName = "";
        /// <summary>
        /// 列表页,生成页的起始位置  从1开始
        /// </summary>
        public int intListPagePubIndex = 1;
        /// <summary>
        /// 分页数据的真实总数量
        /// </summary>
        public int intPageRealCount = 0;
        /// <summary>
        /// list分页的sql
        /// </summary>
        public string strPageWhereSql = "";
        /// <summary>
        /// 搜索页的搜索关键字
        /// </summary>
        public string strSearchPageKeys = "";

        /// <summary>
        /// 生成的页面文件名，去掉后缀
        /// </summary>
        public string strPageName = "";
        /// <summary>
        /// 生成页面的后缀名
        /// </summary>
        public string strPageExtName = "";

        /// <summary>
        /// 主内容字段
        /// </summary>
        public string strMainField = string.Empty;
        /// <summary>
        /// 主内容分页后的数据
        /// </summary>
        public ArrayList oContantStrArray = new ArrayList();

        /// <summary>
        /// 分页类型（动态、静态）
        /// </summary>
        public string strPageingType = string.Empty;

        /// <summary>
        /// 当前内容信息
        /// </summary>
        public DataRow oContentInfoRow = null;
        /// <summary>
        /// 当前节点信息
        /// </summary>
        public DataRow oNodeInfoRow = null;
        /// <summary>
        /// 模板路径
        /// </summary>
        public string strTemplateFilePath = string.Empty;
        /// <summary>
        /// 模板对应的CS文件路径
        /// </summary>
        public string strTemplateCsFilePath = string.Empty;
        /// <summary>
        /// 当前模板路径
        /// </summary>
        public string strCurrentFolder = string.Empty;
        /// <summary>
        /// 模板DLL路径
        /// </summary>
        public string strTemplateDLLPath = string.Empty;
        /// <summary>
        ///  主内容字段
        /// </summary>
        public string strContentMainFied = string.Empty;
        /// <summary>
        /// 是否已生成了CS文件
        /// </summary>
        public bool IsGenerateCsFile = false;
        /// <summary>
        /// 是否生成了CS文件
        /// </summary>
        public bool IsGenerateDllFile = false;
        /// <summary>
        /// 错误信息
        /// </summary>
        public string strErrorInfo = string.Empty;
        /// <summary>
        /// CS程序文件
        /// </summary>
        public StringBuilder strBuildCS = new StringBuilder();
        /// <summary>
        /// 生成的html
        /// </summary>
        public StringBuilder strBuildHtml = new StringBuilder();
        /// <summary>
        /// 应用的模板类型
        /// </summary>
        public EnumList.TemplateType enumTemplateType = EnumList.TemplateType.ContentPage;

        /// <summary>
        /// 页面生成之前预设定的变量集合
        /// </summary>
        public Dictionary<string, string> oPageVars = new Dictionary<string, string>();


        /// <summary>
        /// [辅助信息] 根据指定的TemplateDllPageInfo实体，主函数所花费的总时间
        /// </summary>
        public int intTotalTimeSpan = 0;

        /// <summary>
        /// [辅助信息] 数据访问所需的时间
        /// </summary>
        public int intDataTimeSpan = 0;

        /// <summary>
        /// [辅助信息] 预处理页面所花费的时间
        /// </summary>
        public int intPreExecuteDataTimeSpan = 0;

        /// <summary>
        /// [辅助信息] 最后处理页面所花费的时间
        /// </summary>
        public int intEndExecuteDataTimeSpan = 0;

        /// <summary>
        /// 图片资源类型
        /// </summary>
        public ImgType.ImgResourceType oImgResourceType = ImgType.ImgResourceType.Normal;

        /// <summary>
        /// 高级搜索时所对应的Condation
        /// </summary>
        public string strAdvancedSearchCondation = string.Empty;

    }


    /// <summary>
    ///  模板操作接口
    /// </summary>
    public interface ItemplateControl
    {
        TemplateDllPageInfo Analysis(TemplateDllPageInfo oTemplatePageInfo);
    }
}
